/**
* 添加按钮操作
*/
$("#button-add").click(function(){
    var url = SCOPE.add_url;
    window.location.href=url; // 
});

/*
    提交form表单操作
*/
$("#singcms-button-submit").click(function(){
    //获取form表单的值
    var data = $("#singcms-form").serializeArray();
    //serializeArray();获取form表单中的值并数组化
    postData = {};
    $(data).each(function(i){
        postData[this.name] = this.value;
    });
    /** $(data).each(function(i){
     *   postData[this.name]=this.value;    作用：相当于php中的foreach(){}  
     * });
     * */
    //将获取到的数据POST提交给服务器
    url = SCOPE.save_url;//SCOPE  作用域
    jump_url = SCOPE.jump_url;
    $.post(url,postData,function(res){
        if(res.status === 1){
            //success
           return dialog.success(res.message,jump_url);
        }else if(res.status ===0){
            //error
            return dialog.error(res.message);
        }
    },"JSON");
});
/*
 * 编辑模块
 * 
 */
$('.singcms-table #singcms-edit').on('click',function(){
   var id=$(this).attr('attr-id');//获取属性值（传值id）   
   var url=SCOPE.edit_url+'&id='+id;//链接+id值
   window.location.href=url;
});
/**
 * 
 * 删除模块
 * 
 */
$('.singcms-table #singcms-delete').on('click',function(){
  var id=$(this).attr("attr-id");
  var a=$(this).attr("attr-a");
  var message=$(this).attr("attr-message");
  var url =SCOPE.set_status_url;
  data={};
  data['id']=id;
  data['status']= -1;
  layer.open({
   type:0,
   title:'是否提交?',
   btn:['yes','no'],
   icon:3,
   closeBtn:2,
   content:"是否确定"+message,
   scrollbar:true,
   yes:function(){
    //执行相关跳转
      todelete(url,data);
   }
  });
    
});
function todelete(url,data){
  $.post( url,data,function(s){
       if(s.status===1){
           return dialog.success(s.message,'');
       }else{
           return dialog.error(s.message);
             }
       }
               
     ,"JSON");  
}
/***
 * 排序功能
 * 
 */
$("#button-listorder").click(function(){
   //获取listorder的内容
   var data=$("#singcms-listorder").serializeArray();
   postData={};
   $(data).each(function(i){
       postData[this.name]=this.value;
   });
   var url=SCOPE.listorder_url;
   $.post(url,postData,function(result){
       if(result.status==1){
           //success
           return dialog.success(result.message,result['data']['jump_url']);
       }else if(result.status==0){
          //error
          return dialog.error(result.message,result['data']['jump_url']);
       }
     
   },'JSON');
    
});

/*****
 * 
 * 修改状态
 * 
 */
$('.singcms-table  #singcms-on-off').on('click',function(){
  var id=$(this).attr("attr-id");
  var status =$(this).attr("attr-status");
  var url =SCOPE.set_status_url;
  data={};
  data['id']=id;
  data['status']=status;
  layer.open({
   type:0,
   title:'是否提交?',
   btn:['yes','no'],
   icon:3,
   closeBtn:2, 
   content:"是否确定更改状态？", 
   scrollbar:true,
   yes:function(){
      //执行相关跳转
     todelete(url,data);  
   },
  });
 
});

/*
 * 推送相关JS
 * 
 */

$("#singcms-push").click(function(){
    var id= $("#select-push").val();
    if(id==0){
        return dialog.error("请选择推荐位");
    }
    push={};
    postData={};
    $("input[name='pushcheck']:checked").each(function(i){
        push[i]=$(this).val();
    });
    postData['push']=push;
    postData['position_id']=id;
    //console.log(postData);return;
    var url=SCOPE.push_url;
    $.post(url,postData,function(result){
       if(result.status==1){
         //success
         return dialog.success(result.message,result['data']['jump_url']);
      }else if(result.status==0){
        //error 
        return dialog.error(result.message);
         
     }
        
    },'JSON');
    
});